<template>
  <view class="page">
    <!-- 地址列表 -->
    <scroll-view class="address-list" scroll-y v-if="addressList.length > 0">
      <view 
        class="address-item" 
        v-for="(item, index) in addressList" 
        :key="item.id"
        @click="selectAddress(item)"
      >
        <view class="item-content">
          <view class="item-header">
            <view class="user-info">
              <text class="user-name">{{ item.contactName }}</text>
              <text class="user-phone">{{ item.contactPhone }}</text>
            </view>
            <text class="default-badge" v-if="item.isDefault === 1">默认</text>
          </view>
          
          <view class="item-address">
            <text class="address-icon">📍</text>
            <text class="address-text">{{ formatAddress(item) }}</text>
          </view>
          
          <view class="item-footer">
            <view class="btn-set-default" @click.stop="setDefault(item)" v-if="item.isDefault !== 1">
              <text>设为默认</text>
            </view>
            <view class="item-actions">
              <view class="btn-edit" @click.stop="editAddress(item)">
                <text>编辑</text>
              </view>
              <view class="btn-delete" @click.stop="deleteAddress(item, index)">
                <text>删除</text>
              </view>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>
    
    <!-- 空状态 -->
    <view class="empty" v-else>
      <text class="empty-icon">📍</text>
      <text class="empty-text">还没有收货地址哦</text>
    </view>
    
    <!-- 添加地址按钮 -->
    <view class="bottom-bar">
      <view class="btn-add" @click="addAddress">
        <text class="btn-text">+ 新增地址</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      addressList: [],
      selectMode: false // 是否是选择地址模式
    }
  },
  onLoad(options) {
    if (options.select === 'true') {
      this.selectMode = true
      uni.setNavigationBarTitle({
        title: '选择地址'
      })
    }
  },
  onShow() {
    this.loadAddressList()
  },
  methods: {
    formatAddress(item) {
      let address = ''
      if (item.building) address += item.building
      if (item.room) address += (address ? '-' : '') + item.room
      if (item.detail) address += (address ? ' ' : '') + item.detail
      return address || '暂无详细地址'
    },
    
    async loadAddressList() {
      try {
        const { getAddressList } = require('@/api/address')
        const list = await getAddressList()
        this.addressList = list || []
      } catch (error) {
        console.error('加载地址列表失败:', error)
        uni.showToast({
          title: '加载失败',
          icon: 'none'
        })
      }
    },
    
    selectAddress(item) {
      if (this.selectMode) {
        // 选择地址模式，返回选中的地址
        const pages = getCurrentPages()
        const prevPage = pages[pages.length - 2]
        if (prevPage) {
          prevPage.$vm.selectedAddress = item
        }
        uni.navigateBack()
      }
    },
    
    async setDefault(item) {
      try {
        const { setDefaultAddress } = require('@/api/address')
        await setDefaultAddress(item.id)
        
        uni.showToast({
          title: '设置成功',
          icon: 'success'
        })
        
        this.loadAddressList()
      } catch (error) {
        console.error('设置默认地址失败:', error)
        uni.showToast({
          title: error.message || '设置失败',
          icon: 'none'
        })
      }
    },
    
    addAddress() {
      uni.navigateTo({
        url: '/pages/address/edit'
      })
    },
    
    editAddress(item) {
      uni.navigateTo({
        url: '/pages/address/edit?id=' + item.id
      })
    },
    
    async deleteAddress(item, index) {
      uni.showModal({
        title: '提示',
        content: '确定要删除该地址吗？',
        success: async (res) => {
          if (res.confirm) {
            try {
              const { deleteAddress } = require('@/api/address')
              await deleteAddress(item.id)
              
              uni.showToast({
                title: '已删除',
                icon: 'success'
              })
              
              this.loadAddressList()
            } catch (error) {
              console.error('删除地址失败:', error)
              uni.showToast({
                title: error.message || '删除失败',
                icon: 'none'
              })
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  min-height: 100vh;
  background: #f5f7fa;
  display: flex;
  flex-direction: column;
  padding-bottom: 160rpx;
}

.address-list {
  flex: 1;
  padding: 20rpx;
}

.address-item {
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.item-content {
  padding: 32rpx;
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.user-info {
  display: flex;
  gap: 24rpx;
  align-items: center;
  
  .user-name {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
  }
  
  .user-phone {
    font-size: 28rpx;
    color: #666;
  }
}

.default-badge {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  font-size: 22rpx;
  padding: 8rpx 16rpx;
  border-radius: 8rpx;
}

.item-address {
  display: flex;
  gap: 12rpx;
  align-items: flex-start;
  padding: 20rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
  
  .address-icon {
    font-size: 28rpx;
  }
  
  .address-text {
    flex: 1;
    font-size: 26rpx;
    color: #666;
    line-height: 1.6;
  }
}

.item-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20rpx;
}

.btn-set-default {
  font-size: 26rpx;
  color: #667eea;
}

.item-actions {
  display: flex;
  gap: 32rpx;
  
  .btn-edit,
  .btn-delete {
    font-size: 26rpx;
  }
  
  .btn-edit {
    color: #667eea;
  }
  
  .btn-delete {
    color: #ff4d4f;
  }
}

/* 空状态 */
.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 40rpx;
  
  .empty-icon {
    font-size: 160rpx;
    margin-bottom: 40rpx;
  }
  
  .empty-text {
    font-size: 28rpx;
    color: #999;
  }
}

/* 底部按钮 */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  padding: 24rpx;
  box-shadow: 0 -4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.btn-add {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  height: 88rpx;
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  
  .btn-text {
    font-size: 28rpx;
    color: #fff;
    font-weight: bold;
  }
}
</style>
